package cn.tedu.oa.sys.controller;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.sys.entity.Log;
import cn.tedu.oa.sys.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sys/log/")
public class LogController {

    @Autowired
    private LogService logService;

    @RequestMapping("deleteByLogId")
    @ResponseBody
    public JsonResult deleteByLogId(String id){
        if (id != null) {
            int logId = Integer.parseInt(id);
            logService.delete(logId);
        }
        return new JsonResult("delete ok");
    }


    /*添加数据*/
    @RequestMapping("getAdd")
    @ResponseBody
    public JsonResult getAdd(Log log){
        log.setAddUser(ShiroUtils.getPrincipal().getName());
        log.setAddTime(new Date());
        logService.add(log);
        return new JsonResult("添加成功");
    }

    @RequestMapping("getUpdate")
    @ResponseBody
    public  JsonResult getUpdate(Log log){
        log.setUpdateUser(ShiroUtils.getPrincipal().getName());
        log.setUpdateTime(new Date());
        logService.update(log);
        return new JsonResult("修改成功");
    }


    /**日志首页*/
    @RequestMapping("log_ui")
    public String doLogListUI(){
        return "sys/log/log_ui";
    }

    /*日志編輯頁面*/
    @RequestMapping("log_update")
    public String doLogUpdateUI() {
        return "sys/log/log_update";
    }

    /*日志編輯頁面*/
    @RequestMapping("log_edit")
    public String doLogAddUI() {
        return "sys/log/log_edit";
    }

    /**
     * 日志分页显示
     */
    @RequestMapping("get")
    @ResponseBody
    public JsonResult get(String criteria, String startTime, String endTime, Integer pageNo, Integer pageSize) {
        List<Log> list = logService.getLogPageList(criteria, startTime,endTime,pageNo, pageSize);
        int rowCount = logService.getRowCount(criteria, startTime,endTime);
        JsonResult result = new JsonResult();
        result.setCount(rowCount);
        result.setData(list);
        return result;
    }

    /*根据id删除用户*/
    @RequestMapping("deleteByIds")
    @ResponseBody
    public JsonResult deleteByIds(Integer... ids) {
        logService.deleteByIds(ids);
        return new JsonResult("删除成功");
    }
}
